<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/request.js"></script>
</head>

<body>

</body>
<script>
    // var p = new Promise(function (resolve, reject) {
    //     $.ajax({
    //         type: "get",
    //         url: "../data/1.txt",
    //         success: function (txt) {
    //             resolve(txt);
    //         }
    //     })
    // })

    // p.then(txt => {

    // }).catch(err => {

    // })

    // 多请求链式操作

    var p1 = request({
        type: "get",
        url: "../data/1.txt",
    });

    var p2 = request({
        type: "get",
        url: "../data/2.txt",
    });

    var p3 = request({
        type: "get",
        url: "../data/3.txt",
    });

    console.log(p1);
    console.log(p2);
    console.log(p3);

    var str = "";
    p1.then(txt => {
        console.log("已成功", txt);
        str += txt;
        return p2;
    }).then(txt => {
        console.log("已成功", txt);
        str += txt;
        return p3;
    }).then(txt => {
        console.log("已成功", txt);
        str += txt;
        return str;
    }).then(msg => {
        console.log("已成功", msg);

    }).catch(err => {
        console.log("已失败", err);
    })


    // 对已有的 $.ajax()进行封装  => 模拟jquery
    function readText(url) {
        // 默认返回 pending Promise实例 => 等请求有结果之后 在改变状态
        return new Promise(function (resolve, reject) {
            $.ajax({
                type: "get",
                url: url,
                success: function (txt) {
                    resolve(txt);
                },
                error: function (err) {
                    reject(err);
                }
            })
        })
    }



</script>

</html>